package com.zhn.resourcej.controller;


import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhn.resourcej.entity.User;
import com.zhn.resourcej.service.IUserService;
import com.zhn.resourcej.web.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 90731
 */

@RestController
@Api(tags ="用户")
@RequiredArgsConstructor
public class AccountController {

    private final IUserService userService;

    @Value("${jwt.secret}")
    private String secret;
    @PostMapping("/login")
    @ApiOperation("登录")
    public Result login(String username, String password){
        User u=userService.findByUsnum(username);
        if(u!=null && u.getUspsw().equals(password)){
            //1、生成TOKEN
            Map<String,Object> payload=new ConcurrentHashMap<>();
            payload.put("username", username);
            payload.put("id", u.getUsid());
            String token= JWTUtil.createToken(payload,secret.getBytes());;
            //2、返回前端相关信息
            return Result.success("登录成功",token);
        }
        return Result.fail(101,"用户名或密码错误");
    }
}
